package com.topscore.uke.mvp.presenters;

import com.topscore.uke.entities.ConsumptionItemBean;
import com.topscore.uke.entities.MemberConsumpInfo;
import com.topscore.uke.mvp.contractors.GetMemberConsumpContractor;
import com.topscore.uke.mvp.model.GetMemberConsumpModel;
import com.topscore.uke.network.BaseResponse;
import com.topscore.uke.network.request.MemberConsumpRequest;

public class GetMemberConsumpPresenter implements GetMemberConsumpContractor, GetMemberConsumpContractor.Presenter {

    private MemberConsumpRequest mRequest;

    private View mView;

    private final int _PageSize = 20;

    private int mCurrentPage = 1;

    private Model mModel;

    public GetMemberConsumpPresenter(String phone, View view) {
        this.mView = view;
        this.mRequest = new MemberConsumpRequest();
//        mRequest.setPhone(phone);
        mRequest.setPageSize(_PageSize);

    }

    @Override
    public void getMemberConsump() {
        mCurrentPage = 1;
        mRequest.setCurrentPage(mCurrentPage);
        mView.onLoading("");
        mModel = new GetMemberConsumpModel(new Model.CallBack() {
            @Override
            public void onSuccess(BaseResponse<MemberConsumpInfo> response) {
                if (!response.getData().getDataList().isEmpty()) {
                    mView.getRecyclerAdapter().setNewData(response.getData().getDataList());
                    if (response.getData().getDataList().size() < _PageSize) {
                        mView.getRecyclerAdapter().loadMoreComplete();
                        mView.getRecyclerAdapter().loadMoreEnd();
                    }
                }
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                mView.onFail(errorCode, errorMessage);
            }

            @Override
            public void onComplete() {
                mView.onLoadFinished();
            }
        });
        mModel.getData(mRequest);
    }

    @Override
    public void loadNextPage() {
        int page = mCurrentPage + 1;
        mRequest.setCurrentPage(page);
        mModel = new GetMemberConsumpModel(new Model.CallBack() {

            @Override
            public void onSuccess(BaseResponse<MemberConsumpInfo> response) {
                if (!response.getData().getDataList().isEmpty()) {
                    mCurrentPage = page;
                    mView.getRecyclerAdapter().addData(response.getData().getDataList());
                } else {
                    mView.getRecyclerAdapter().loadMoreComplete();
                    mView.getRecyclerAdapter().loadMoreEnd();
                }
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
//                mView.onFail(errorCode, errorMessage);
                mView.getRecyclerAdapter().loadMoreComplete();
                mView.getRecyclerAdapter().loadMoreFail();
            }

            @Override
            public void onComplete() {
                mView.getRecyclerAdapter().loadMoreComplete();
            }
        });
        mModel.getData(mRequest);
    }
}
